﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
namespace Business
{
   public class Notification
    {
       public bool Sendmail(string message,MailAddress to,MailAddress cc,MailAddress bcc)
       {
           try
           {
               bool result ;

               //DataAccess.TblContract con=new DataAccess.TblContract();
               //con= DataAccess.TblContract.FetchByID(contractid);
               //con.
               System.Web.Mail.SmtpMail.SmtpServer = "192.168.0.5:25";
               SmtpClient smtp = new SmtpClient();
               using (System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage())
               {
                   smtp.Host = "192.168.0.5";
                   smtp.Port = 25;
                   smtp.Credentials = new System.Net.NetworkCredential("fpm", "fpm");
                   smtp.EnableSsl = false;
                   mailMessage.To.Add(to);
                   mailMessage.CC.Add(cc);
                   mailMessage.Bcc.Add(bcc);
                   mailMessage.Subject = "سیستم مدیریت پروژه بنیاد مسکن انقلاب اسلامی فارس";
                   mailMessage.Body = message;
                   mailMessage.From = new System.Net.Mail.MailAddress("fpm@bonyadmaskanfars.ir");
                   smtp.Send(mailMessage);
                  
                   return true;
               }
           }
           catch
           {
               return false;
           }
       }
       public bool SaveInnerNotific(Guid ContractId,Guid stepid,bool ContractType ,Guid userid)
       {
           try
           {
               string message = "";
               DataAccess.TblContract con = new DataAccess.TblContract();
               DataAccess.TblStepsAdvisor advi = new DataAccess.TblStepsAdvisor();
               DataAccess.TblStepsSurveyor surv = new DataAccess.TblStepsSurveyor();
               DataAccess.TblVillage VIL = new DataAccess.TblVillage();
               DataAccess.TblDehestan deh = new DataAccess.TblDehestan();
               DataAccess.TblBakhsh bakhsh = new DataAccess.TblBakhsh();
               DataAccess.TblCity city = new DataAccess.TblCity();
               con = DataAccess.TblContract.FetchByID(ContractId);
               VIL = DataAccess.TblVillage.FetchByID(con.VillageId);
               deh = DataAccess.TblDehestan.FetchByID(VIL.DehstanId);
               bakhsh = DataAccess.TblBakhsh.FetchByID(deh.BakhshId);
               city = DataAccess.TblCity.FetchByID(bakhsh.CityId);
              
               IDataReader dr = DataAccess.TblUserDetail.FetchByParameter(DataAccess.TblUserDetail.Columns.UserId, con.UserId);
               string s=con.ContractNumber;
                     string[] split = s.Split(new Char[] { '/' });
                     string contractnumber = split[2].ToString()+"/" + split[1].ToString()+"/" + split[0].ToString();

               if (ContractType == true)
               {
                   advi = DataAccess.TblStepsAdvisor.FetchByID(stepid);
                   message = " مدارک مربوط به مرحله " + advi.Name + " از قرارداد شماره " + contractnumber + " شهرستان " + city.Name + " روستای " + VIL.Name + " مشاور " + Business.CommonCalculations.FindDoerOfProject(ContractId) + " ارسال شد. ";
               }
               else if (ContractType == false)
               {
                   surv = DataAccess.TblStepsSurveyor.FetchByID(stepid);
                   message = " مدارک مربوط به مرحله " + surv.Name + " از قرارداد شماره " + contractnumber + " شهرستان " + city.Name + " روستای " + VIL.Name + " نقشه بردار " + Business.CommonCalculations.FindDoerOfProject(ContractId) + " ارسال شد. ";

               } 
               DataAccess.TblNotification notific = new DataAccess.TblNotification();
               notific.UserId = userid;
               notific.Message = message;
               notific.IsRead = false;
               notific.DateX = DateTime.Now;
               notific.Save();
               return true;
           }
           catch 
           {
               return false;
           }
       }
       public bool SaveInnerNotificEnd(Guid ContractId, Guid stepid, bool ContractType, Guid userid)
       {
           try
           {
               string message = "";
               DataAccess.TblContract con = new DataAccess.TblContract();
               DataAccess.TblStepsAdvisor advi = new DataAccess.TblStepsAdvisor();
               DataAccess.TblStepsSurveyor surv = new DataAccess.TblStepsSurveyor();
               DataAccess.TblVillage VIL = new DataAccess.TblVillage();
               DataAccess.TblDehestan deh = new DataAccess.TblDehestan();
               DataAccess.TblBakhsh bakhsh = new DataAccess.TblBakhsh();
               DataAccess.TblCity city = new DataAccess.TblCity();
               con = DataAccess.TblContract.FetchByID(ContractId);
               VIL = DataAccess.TblVillage.FetchByID(con.VillageId);
               deh = DataAccess.TblDehestan.FetchByID(VIL.DehstanId);
               bakhsh = DataAccess.TblBakhsh.FetchByID(deh.BakhshId);
               city = DataAccess.TblCity.FetchByID(bakhsh.CityId);

               IDataReader dr = DataAccess.TblUserDetail.FetchByParameter(DataAccess.TblUserDetail.Columns.UserId, con.UserId);
               string s = con.ContractNumber;
               string[] split = s.Split(new Char[] { '/' });
               string contractnumber = split[2].ToString() + "/" + split[1].ToString() + "/" + split[0].ToString();

               if (ContractType == true)
               {
                   advi = DataAccess.TblStepsAdvisor.FetchByID(stepid);
                   message = "  مرحله " + advi.Name + " از قرارداد شماره " +  contractnumber + " شهرستان " + city.Name + " روستای " + VIL.Name + " از طرف مشاور " + Business.CommonCalculations.FindDoerOfProject(ContractId) + " پایان یافته اعلام شد. ";
               }
               else if (ContractType == false)
               {
                   surv = DataAccess.TblStepsSurveyor.FetchByID(stepid);
                   message = "  مرحله " + surv.Name + " از قرارداد شماره " +contractnumber + " شهرستان " + city.Name + " روستای " + VIL.Name + " از طرف نقشه بردار " + Business.CommonCalculations.FindDoerOfProject(ContractId) + " پایان یافته اعلام شد. ";

               }
               DataAccess.TblNotification notific = new DataAccess.TblNotification();
               notific.UserId = userid;
               notific.Message = message;
               notific.IsRead = false;
               notific.DateX = DateTime.Now;
               notific.Save();
               return true;
           }
           catch
           {
               return false;
           }
       }
       public string Returnmessage(Guid ContractId, Guid stepid, bool ContractType)
       {
           try
           {
               string message = "";
               DataAccess.TblContract con = new DataAccess.TblContract();
               DataAccess.TblStepsAdvisor advi = new DataAccess.TblStepsAdvisor();
               DataAccess.TblStepsSurveyor surv = new DataAccess.TblStepsSurveyor();
               DataAccess.TblVillage VIL = new DataAccess.TblVillage();
               DataAccess.TblDehestan deh = new DataAccess.TblDehestan();
               DataAccess.TblBakhsh bakhsh = new DataAccess.TblBakhsh();
               DataAccess.TblCity city = new DataAccess.TblCity();
               con = DataAccess.TblContract.FetchByID(ContractId);
               VIL = DataAccess.TblVillage.FetchByID(con.VillageId);
               deh = DataAccess.TblDehestan.FetchByID(VIL.DehstanId);
               bakhsh = DataAccess.TblBakhsh.FetchByID(deh.BakhshId);
               city = DataAccess.TblCity.FetchByID(bakhsh.CityId);
       
               IDataReader dr= DataAccess.TblUserDetail.FetchByParameter(DataAccess.TblUserDetail.Columns.UserId, con.UserId);
               string s = con.ContractNumber;
               string[] split = s.Split(new Char[] { '/' });
               string contractnumber = split[2].ToString() + "/" + split[1].ToString() + "/" + split[0].ToString();

                   if (ContractType == true)
               {
                   advi = DataAccess.TblStepsAdvisor.FetchByID(stepid);
                   message = " مدارک مربوط به مرحله " + advi.Name + " از قرارداد شماره " +contractnumber+" شهرستان "+ city.Name + " روستای " + VIL.Name +" مشاور "+ Business.CommonCalculations.FindDoerOfProject(ContractId)+ " ارسال شد. ";
               }
               else if (ContractType == false)
               {
                   surv = DataAccess.TblStepsSurveyor.FetchByID(stepid);
                   message = " مدارک مربوط به مرحله " + surv.Name + " از قرارداد شماره " + contractnumber + " شهرستان " + city.Name + " روستای " + VIL.Name + " نقشه بردار " + Business.CommonCalculations.FindDoerOfProject(ContractId) + " ارسال شد. ";
           
               }
              
               return message;
           }
           catch
           {
               return "خطا در ساختن پیغام";
           }
       }
       public bool SaveInnerNotific(Guid ContractId, Guid stepid, bool ContractType, Guid userid,bool Control,string reason)
       {
           try
           {
               string message = "";
               string controlstr = "";
               DataAccess.TblContract con = new DataAccess.TblContract();
               DataAccess.TblStepsAdvisor advi = new DataAccess.TblStepsAdvisor();
               DataAccess.TblStepsSurveyor surv = new DataAccess.TblStepsSurveyor();
               DataAccess.TblVillage VIL = new DataAccess.TblVillage();
               DataAccess.TblDehestan deh = new DataAccess.TblDehestan();
               DataAccess.TblBakhsh bakhsh = new DataAccess.TblBakhsh();
               DataAccess.TblCity city = new DataAccess.TblCity();
               con = DataAccess.TblContract.FetchByID(ContractId);
               VIL = DataAccess.TblVillage.FetchByID(con.VillageId);
               deh = DataAccess.TblDehestan.FetchByID(VIL.DehstanId);
               bakhsh = DataAccess.TblBakhsh.FetchByID(deh.BakhshId);
               city = DataAccess.TblCity.FetchByID(bakhsh.CityId);
               string s = con.ContractNumber;
               string[] split = s.Split(new Char[] { '/' });
               string contractnumber = split[2].ToString() + "/" + split[1].ToString() + "/" + split[0].ToString();

               if(Control==true)
                    controlstr="تایید شد.";
               if (Control == false )
                   controlstr = "تایید نشد.";
               if (ContractType == true)
               {
                   advi = DataAccess.TblStepsAdvisor.FetchByID(stepid);
                   message = "  مرحله " + advi.Name + " از قرارداد شماره " + contractnumber + " شهرستان " + city.Name + " روستای " + VIL.Name + " مشاور " + Business.CommonCalculations.FindDoerOfProject(ContractId) + " " + " (به دلیل " + reason + ") " + controlstr;
               }
               else if (ContractType == false)
               {
                   surv = DataAccess.TblStepsSurveyor.FetchByID(stepid);
                   message = " مرحله " + surv.Name + " از قرارداد شماره " + contractnumber + " شهرستان " + city.Name + " روستای " + VIL.Name + " نقشه بردار " + Business.CommonCalculations.FindDoerOfProject(ContractId) + " " + " (به دلیل " + reason + ") " + controlstr;

               }
               DataAccess.TblNotification notific = new DataAccess.TblNotification();
               notific.UserId = userid;
               notific.Message = message;
               notific.IsRead = false;
               notific.DateX = DateTime.Now;
               notific.Save();
               return true;
           }
           catch
           {
               return false;
           }
       }

       public string Returnmessagereason(Guid ContractId, Guid stepid, bool ContractType,  bool Control, string reason)
       {
           try
           {
               string message = "";
               string controlstr = "";
               DataAccess.TblContract con = new DataAccess.TblContract();
               DataAccess.TblStepsAdvisor advi = new DataAccess.TblStepsAdvisor();
               DataAccess.TblStepsSurveyor surv = new DataAccess.TblStepsSurveyor();
               DataAccess.TblVillage VIL = new DataAccess.TblVillage();
               DataAccess.TblDehestan deh = new DataAccess.TblDehestan();
               DataAccess.TblBakhsh bakhsh = new DataAccess.TblBakhsh();
               DataAccess.TblCity city = new DataAccess.TblCity();
               con = DataAccess.TblContract.FetchByID(ContractId);
               VIL = DataAccess.TblVillage.FetchByID(con.VillageId);
               deh = DataAccess.TblDehestan.FetchByID(VIL.DehstanId);
               bakhsh = DataAccess.TblBakhsh.FetchByID(deh.BakhshId);
               city = DataAccess.TblCity.FetchByID(bakhsh.CityId);
               string s = con.ContractNumber;
               string[] split = s.Split(new Char[] { '/' });
               string contractnumber = split[2].ToString() + "/" + split[1].ToString() + "/" + split[0].ToString();

               if (Control == true)
                   controlstr = "تایید شد.";
               if (Control == false)
                   controlstr = "تایید نشد.";
               if (ContractType == true)
               {
                   advi = DataAccess.TblStepsAdvisor.FetchByID(stepid);
                   message = "  مرحله " + advi.Name + " از قرارداد شماره " + contractnumber + " شهرستان " + city.Name + " روستای " + VIL.Name + " مشاور " + Business.CommonCalculations.FindDoerOfProject(ContractId) + " " + " (به دلیل " + reason + ") " + controlstr;
               }
               else if (ContractType == false)
               {
                   surv = DataAccess.TblStepsSurveyor.FetchByID(stepid);
                   message = " مرحله " + surv.Name + " از قرارداد شماره " + contractnumber + " شهرستان " + city.Name + " روستای " + VIL.Name + " نقشه بردار " + Business.CommonCalculations.FindDoerOfProject(ContractId) + " " + " (به دلیل " + reason + ") " + controlstr;

               }
               return message;
           }
           catch
           {
               return "خطا در ساختن پیغام";
           }
       }

       public Guid findbonyadshahrestan(Guid contractid)
       {
           Guid useridbonyad = Guid.Empty ;
           DataAccess.TblContract con = new DataAccess.TblContract();
           DataAccess.TblDehestan deh = new DataAccess.TblDehestan();
           DataAccess.TblBakhsh bakhsh = new DataAccess.TblBakhsh();
           DataAccess.TblVillage vil = new DataAccess.TblVillage();
           DataAccess.TblCity city = new DataAccess.TblCity();
           con=DataAccess.TblContract.FetchByID(contractid);
           vil=DataAccess.TblVillage.FetchByID(con.VillageId);
           deh = DataAccess.TblDehestan.FetchByID(vil.DehstanId);
           bakhsh=DataAccess.TblBakhsh.FetchByID(deh.BakhshId);
           city = DataAccess.TblCity.FetchByID(bakhsh.CityId);
           IDataReader dr= DataAccess.TblUserDetail.FetchByParameter(DataAccess.TblUserDetail.Columns.CityId, city.Id);
           if (dr.Read())
           {
               useridbonyad = new Guid(dr["UserId"].ToString());
               return useridbonyad;
               dr.Close();
           }
           else
           {
               return useridbonyad;
           }
       }
       public string[] FindMobile(List<string> userids)
       {
           List<string> slist=new List<string>();
       
           string[] s;
           for (int i = 0; i <= userids.Count-1; i++)
           {
               
               IDataReader dr= DataAccess.TblUserDetail.FetchByParameter(DataAccess.TblUserDetail.Columns.UserId, userids[i]);
               if (dr.Read())
                   if(dr["Mobile"].ToString()!="")
                  slist.Add( dr["Mobile"].ToString());
           }
           s = slist.ToArray();
               return s;
       }
    }
}
